/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "imageknifec/imageknifec_image_data.h"
#include "image_data.h"
#include "imageknife_wrapper.h"

using namespace ImageKnifePro;
extern "C" {
ImageknifecError ImageknifecImageDataGetFrameCount(ImageknifecImageData *imageData, uint32_t *frameCount)
{
    if (imageData == nullptr || frameCount == nullptr) {
        return IMAGE_KNIFE_ERROR_BAD_PARAMETER;
    }

    auto imageDataPtr = reinterpret_cast<ImageKnifeWrapper<ImageData> *>(imageData)->GetPtr();
    if (imageDataPtr == nullptr) {
        return IMAGE_KNIFE_ERROR_OBJECT_NOT_EXIST;
    }

    *frameCount = imageDataPtr->GetFrameCount();
    if (frameCount == 0) {
        return IMAGE_KNIFE_ERROR_OBJECT_EMPTY_DATA;
    }

    return IMAGE_KNIFE_SUCCESS;
}

ImageknifecError ImageknifecImageDataGetPixelmap(ImageknifecImageData *imageData, OH_PixelmapNative **pixelmap)
{
    if (imageData == nullptr || pixelmap == nullptr) {
        return IMAGE_KNIFE_ERROR_BAD_PARAMETER;
    }

    auto imageDataPtr = reinterpret_cast<ImageKnifeWrapper<ImageData> *>(imageData)->GetPtr();
    if (imageDataPtr == nullptr) {
        return IMAGE_KNIFE_ERROR_OBJECT_NOT_EXIST;
    }

    *pixelmap = imageDataPtr->GetPixelmap();
    if (pixelmap == nullptr) {
        return IMAGE_KNIFE_ERROR_OBJECT_EMPTY_DATA;
    }

    return IMAGE_KNIFE_SUCCESS;
}

ImageknifecError ImageknifecImageDataGetPixelmapList(ImageknifecImageData *imageData,
                                                     OH_PixelmapNative *pixelmapList[], size_t size)
{
    if (imageData == nullptr || pixelmapList == nullptr || size == 0) {
        return IMAGE_KNIFE_ERROR_BAD_PARAMETER;
    }

    auto imageDataPtr = reinterpret_cast<ImageKnifeWrapper<ImageData> *>(imageData)->GetPtr();
    if (imageDataPtr == nullptr) {
        return IMAGE_KNIFE_ERROR_OBJECT_NOT_EXIST;
    }

    OH_PixelmapNative **pixelmapListFrom =  imageDataPtr->GetPixelmapList();
    uint32_t frameCount = imageDataPtr->GetFrameCount();
    if (pixelmapListFrom == nullptr || frameCount == 0) {
        return IMAGE_KNIFE_ERROR_OBJECT_EMPTY_DATA;
    }

    for (int i = 0; i < size && i < frameCount; i++) {
        pixelmapList[i] = pixelmapListFrom[i];
    }
    return IMAGE_KNIFE_SUCCESS;
}

ImageknifecError ImageknifecImageDataGetDelayTimeList(ImageknifecImageData *imageData,
                                                      int delayTimeList[], size_t size)
{
    if (imageData == nullptr || delayTimeList == nullptr || size == 0) {
        return IMAGE_KNIFE_ERROR_BAD_PARAMETER;
    }

    auto imageDataPtr = reinterpret_cast<ImageKnifeWrapper<ImageData> *>(imageData)->GetPtr();
    if (imageDataPtr == nullptr) {
        return IMAGE_KNIFE_ERROR_OBJECT_NOT_EXIST;
    }

    int  *delayTimeListFrom =  imageDataPtr->GetDelayTimeList();
    uint32_t frameCount = imageDataPtr->GetFrameCount();
    if (delayTimeListFrom == nullptr || frameCount == 0) {
        return IMAGE_KNIFE_ERROR_OBJECT_EMPTY_DATA;
    }

    for (int i = 0; i < size && i < imageDataPtr->GetFrameCount(); i++) {
        delayTimeList[i] = delayTimeListFrom[i];
    }
    return IMAGE_KNIFE_SUCCESS;
}

ImageknifecError ImageknifecImageDataRelease(ImageknifecImageData *imageData)
{
    if (imageData == nullptr) {
        return IMAGE_KNIFE_ERROR_BAD_PARAMETER;
    }

    delete reinterpret_cast<ImageKnifeWrapper<ImageData> *>(imageData);
    return IMAGE_KNIFE_SUCCESS;
}
} // end of extern "C"